package com.syezon.wifikey.setting;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.ToastUtils;
import com.syezon.wifikey.R;
import com.syezon.wifikey.api.BaseHttpResult;
import com.syezon.wifikey.base.BaseActivity;
import com.syezon.wifikey.utils.StatusBarUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class FeedBackActivity extends BaseActivity {

    @BindView(R.id.ll_top)
    LinearLayout mLlTop;

    @BindView(R.id.edit_contact)
    EditText mEditContact;
    @BindView(R.id.edit_content)
    EditText mEditContent;

    private FeedBackViewModel mFeedBackViewModel;
    private boolean mSubmited;

    public static void feedBack(Context context) {
        Intent intent = new Intent(context, FeedBackActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.app_setting_feedback_activity;
    }


    @Override
    protected void initViewsAndEvents() {
        StatusBarUtil.setPaddingSmart(this, mLlTop);
        StatusBarUtil.darkMode(this, true);
        mFeedBackViewModel = ViewModelProviders.of(this).get(FeedBackViewModel.class);
        mFeedBackViewModel.getFeedBackResultLiveData().observe(this, new Observer<BaseHttpResult<Void>>() {
            @Override
            public void onChanged(@Nullable BaseHttpResult<Void> voidBaseHttpResult) {
                if (voidBaseHttpResult != null) {
                    int rc = voidBaseHttpResult.getRc();
                    int message = voidBaseHttpResult.getMessage();
                    if (rc == BaseHttpResult.RC_OK && message == BaseHttpResult.MSG_OK) {
                        readyGoThenKill(FeedBackOverActivity.class);
                    } else {
                        mSubmited = false;
                        ToastUtils.showShort("提交失败，请稍后重试！");
                    }
                } else {
                    mSubmited = false;
                    ToastUtils.showShort("提交失败，请稍后重试！");
                }
            }
        });
    }

    private boolean checkFeedBack(String contact, String content) {
        if (contact == null || contact.length() < 5) {
            ToastUtils.showShort("QQ号不能少于5位！");
            return false;
        }
        if (content == null || content.length() < 2) {
            ToastUtils.showShort("内容不能少于2个字！");
            return false;
        }
        return true;
    }

    public void submit() {
        String contact = mEditContact.getText().toString();
        String content = mEditContent.getText().toString();
        boolean canSubmit = checkFeedBack(contact, content);
        if (canSubmit && !mSubmited) {
            mSubmited = true;
            mFeedBackViewModel.feedBack(contact, content);
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_submit:
                submit();
                break;
        }
    }
}
